package com.eva.admin.api.system;

import com.eva.framework.common.api.BaseController;
import com.eva.admin.biz.system.SystemJobBiz;
import com.eva.admin.biz.system.dto.CreateSystemJobDTO;
import com.eva.job.SystemJobTrigger;
import com.eva.job.dto.TriggerJobDTO;
import com.eva.admin.biz.system.dto.UpdateSystemJobDTO;
import com.eva.admin.biz.system.dto.UpdateSystemJobStatusDTO;
import com.eva.admin.dao.system.dto.QuerySystemJobDTO;
import com.eva.dao.system.model.SystemJob;
import com.eva.admin.dao.system.vo.SystemJobVO;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.framework.common.utils.Utils;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.rbac.trace.Trace;
import com.eva.job.constants.JobConstants;
import com.eva.service.system.SystemJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/job")
@Api(tags = "定时任务列表接口")
public class SystemJobController extends BaseController {

    @Resource
    private SystemJobService systemJobService;

    @Resource
    private SystemJobBiz systemJobBiz;

    @Resource
    private SystemJobTrigger systemJobTrigger;

    @Trace
    @ApiOperation("手动触发")
    @PostMapping("/trigger")
    @ContainPermissions("system:job:trigger")
    public ApiResponse<?> triggerJob (@RequestBody TriggerJobDTO dto) {
        Utils.ThreadPool.start(() -> {
            dto.setTriggerType(JobConstants.TriggerType.MANUAL.getCode());
            dto.setFireTime(new Date());
            systemJobTrigger.trigger(dto);
        });
        return ApiResponse.success(null);
    }

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("system:job:create")
    public ApiResponse<?> create(@RequestBody CreateSystemJobDTO dto) {
        systemJobBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @ContainPermissions("system:job:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        systemJobBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @ContainPermissions("system:job:delete")
    public ApiResponse<?> deleteByIds(@RequestParam String ids) {
        systemJobBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @ContainPermissions("system:job:update")
    public ApiResponse<?> updateById(@RequestBody UpdateSystemJobDTO dto) {
        systemJobBiz.updateById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("修改任务状态")
    @PostMapping("/updateStatus")
    @ContainPermissions("system:job:update")
    public ApiResponse<?> updateStatus(@RequestBody UpdateSystemJobStatusDTO dto) {
        systemJobBiz.updateStatusById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("system:job:query")
    public ApiResponse<PageData<SystemJobVO>> findPage (@RequestBody PageWrap<QuerySystemJobDTO> pageWrap) {
        return ApiResponse.success(systemJobBiz.findPage(pageWrap));
    }

    @ApiOperation("查询所有")
    @GetMapping("/all")
    @ContainPermissions("system:job:query")
    public ApiResponse<List<SystemJob>> findAll () {
        return ApiResponse.success(systemJobService.findList(new SystemJob()));
    }
}
